<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>SpringMVC-day01 | 阿毛的博客</title><meta name="keywords" content="springMVC"><meta name="author" content="myl"><meta name="copyright" content="myl"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="1 SpringMVC 概述三层架构      表现层：负责数据展示  业务层：负责业务处理  数据层：负责数据操作    MVC（Model View Controller），一种用于设计创建Web应用程序表现层的模式  Model（模型）：数据模型，用于封装数据  View（视图）：页面视图，用于展示数据    jsp   html  Controller（控制器）：处理用户交互的调度器，用于">
<meta property="og:type" content="article">
<meta property="og:title" content="SpringMVC-day01">
<meta property="og:url" content="https://gitee.com/tgreen/myl-mm.git/2022/05/09/springMVCday01/index.html">
<meta property="og:site_name" content="阿毛的博客">
<meta property="og:description" content="1 SpringMVC 概述三层架构      表现层：负责数据展示  业务层：负责业务处理  数据层：负责数据操作    MVC（Model View Controller），一种用于设计创建Web应用程序表现层的模式  Model（模型）：数据模型，用于封装数据  View（视图）：页面视图，用于展示数据    jsp   html  Controller（控制器）：处理用户交互的调度器，用于">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://itcast-haoke-myl.oss-cn-beijing.aliyuncs.com/images/2022/04/13/cat1.jpg">
<meta property="article:published_time" content="2022-05-09T08:58:07.647Z">
<meta property="article:modified_time" content="2022-05-09T11:35:32.844Z">
<meta property="article:author" content="myl">
<meta property="article:tag" content="springMVC">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://itcast-haoke-myl.oss-cn-beijing.aliyuncs.com/images/2022/04/13/cat1.jpg"><link rel="shortcut icon" href="/myl-mm/img/favicon.png"><link rel="canonical" href="https://gitee.com/tgreen/myl-mm.git/2022/05/09/springMVCday01/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/myl-mm/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/myl-mm/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'SpringMVC-day01',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2022-05-09 19:35:32'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><meta name="generator" content="Hexo 5.4.2"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="https://i.loli.net/2021/02/24/5O1day2nriDzjSu.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/myl-mm/archives/"><div class="headline">文章</div><div class="length-num">22</div></a><a href="/myl-mm/tags/"><div class="headline">标签</div><div class="length-num">11</div></a><a href="/myl-mm/categories/"><div class="headline">分类</div><div class="length-num">7</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/myl-mm/"><i class="fa-fw fas fa-home"></i><span> Home</span></a></div><div class="menus_item"><a class="site-page" href="/myl-mm/tags/"><i class="fa-fw fas fa-tags"></i><span> Tags</span></a></div><div class="menus_item"><a class="site-page" href="/myl-mm/categories/"><i class="fa-fw fas fa-folder-open"></i><span> Categories</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://itcast-haoke-myl.oss-cn-beijing.aliyuncs.com/images/2022/04/13/long1.jpg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/myl-mm/">阿毛的博客</a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/myl-mm/"><i class="fa-fw fas fa-home"></i><span> Home</span></a></div><div class="menus_item"><a class="site-page" href="/myl-mm/tags/"><i class="fa-fw fas fa-tags"></i><span> Tags</span></a></div><div class="menus_item"><a class="site-page" href="/myl-mm/categories/"><i class="fa-fw fas fa-folder-open"></i><span> Categories</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">SpringMVC-day01</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2022-05-09T08:58:07.647Z" title="发表于 2022-05-09 16:58:07">2022-05-09</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2022-05-09T11:35:32.844Z" title="更新于 2022-05-09 19:35:32">2022-05-09</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/myl-mm/categories/spring/">spring</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="SpringMVC-day01"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="1-SpringMVC-概述"><a href="#1-SpringMVC-概述" class="headerlink" title="1 SpringMVC 概述"></a>1 SpringMVC 概述</h1><p><strong>三层架构</strong>    </p>
<ul>
<li><p>表现层：负责数据展示</p>
</li>
<li><p>业务层：负责业务处理</p>
</li>
<li><p>数据层：负责数据操作</p>
</li>
</ul>
<p><img src="/myl-mm/day01.assets/image-20200426174726083.png" alt="image-20200426174726083"></p>
<p>MVC（Model View Controller），一种用于设计创建Web应用程序表现层的模式</p>
<ul>
<li><p>Model（模型）：数据模型，用于封装数据</p>
</li>
<li><p>View（视图）：页面视图，用于展示数据</p>
</li>
</ul>
<ul>
<li>jsp  </li>
<li>html</li>
</ul>
<p>Controller（控制器）：处理用户交互的调度器，用于根据用户需求处理程序逻辑</p>
<ul>
<li>Servlet</li>
<li>SpringMVC</li>
</ul>
<p><img src="/myl-mm/day01.assets/image-20200426180210590.png" alt="image-20200426180210590">  </p>
<p><img src="/myl-mm/day01.assets/image-20200426180348299.png" alt="image-20200426180348299"></p>
<h1 id="2-入门案例"><a href="#2-入门案例" class="headerlink" title="2 入门案例"></a>2 入门案例</h1><h2 id="2-1-入门案例制作"><a href="#2-1-入门案例制作" class="headerlink" title="2.1 入门案例制作"></a>2.1 入门案例制作</h2><p>①导入SpringMVC相关坐标</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- servlet3.1规范的坐标 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>javax.servlet<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>javax.servlet-api<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>3.1.0<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">scope</span>&gt;</span>provided<span class="tag">&lt;/<span class="name">scope</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"><span class="comment">&lt;!--jsp坐标--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>javax.servlet.jsp<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>jsp-api<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>2.1<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">scope</span>&gt;</span>provided<span class="tag">&lt;/<span class="name">scope</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"><span class="comment">&lt;!--spring的坐标--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-context<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>5.1.9.RELEASE<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"><span class="comment">&lt;!--spring web的坐标--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-web<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>5.1.9.RELEASE<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"><span class="comment">&lt;!--springmvc的坐标--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-webmvc<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>5.1.9.RELEASE<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>②定义表现层业务处理器Controller，并配置成spring的bean（等同于Servlet）</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Controller</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">UserController</span> &#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">save</span><span class="params">()</span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;user mvc controller is running ...&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?xml version=<span class="string">&quot;1.0&quot;</span> encoding=<span class="string">&quot;UTF-8&quot;</span>?&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">beans</span> <span class="attr">xmlns</span>=<span class="string">&quot;http://www.springframework.org/schema/beans&quot;</span></span></span><br><span class="line"><span class="tag">       <span class="attr">xmlns:context</span>=<span class="string">&quot;http://www.springframework.org/schema/context&quot;</span></span></span><br><span class="line"><span class="tag">       <span class="attr">xmlns:xsi</span>=<span class="string">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span></span><br><span class="line"><span class="tag">       <span class="attr">xsi:schemaLocation</span>=<span class="string">&quot;http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd</span></span></span><br><span class="line"><span class="string"><span class="tag">        http://www.springframework.org/schema/context</span></span></span><br><span class="line"><span class="string"><span class="tag">        http://www.springframework.org/schema/context/spring-context.xsd&quot;</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--扫描加载所有的控制类类--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">context:component-scan</span> <span class="attr">base-package</span>=<span class="string">&quot;com.itheima&quot;</span>/&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;/<span class="name">beans</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>③web.xml中配置SpringMVC核心控制器，用于将请求转发到对应的具体业务处理器Controller中（等同于Servlet配置）</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">servlet</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">servlet-name</span>&gt;</span>DispatcherServlet<span class="tag">&lt;/<span class="name">servlet-name</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">servlet-class</span>&gt;</span>org.springframework.web.servlet.DispatcherServlet<span class="tag">&lt;/<span class="name">servlet-class</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">init-param</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">param-name</span>&gt;</span>contextConfigLocation<span class="tag">&lt;/<span class="name">param-name</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">param-value</span>&gt;</span>classpath*:spring-mvc.xml<span class="tag">&lt;/<span class="name">param-value</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">init-param</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">servlet</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">servlet-mapping</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">servlet-name</span>&gt;</span>DispatcherServlet<span class="tag">&lt;/<span class="name">servlet-name</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">url-pattern</span>&gt;</span>/<span class="tag">&lt;/<span class="name">url-pattern</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">servlet-mapping</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>④设定具体Controller的访问路径（等同于Servlet在web.xml中的配置）</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//设定当前方法的访问映射地址</span></span><br><span class="line"><span class="meta">@RequestMapping(&quot;/save&quot;)</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">save</span><span class="params">()</span>&#123;</span><br><span class="line">    System.out.println(<span class="string">&quot;user mvc controller is running ...&quot;</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>⑤设置返回页面  </p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//设定当前方法的访问映射地址</span></span><br><span class="line"><span class="meta">@RequestMapping(&quot;/save&quot;)</span></span><br><span class="line"><span class="comment">//设置当前方法返回值类型为String，用于指定请求完成后跳转的页面</span></span><br><span class="line"><span class="keyword">public</span> String <span class="title function_">save</span><span class="params">()</span>&#123;</span><br><span class="line">    System.out.println(<span class="string">&quot;user mvc controller is running ...&quot;</span>);</span><br><span class="line">    <span class="comment">//设定具体跳转的页面</span></span><br><span class="line">    <span class="keyword">return</span> <span class="string">&quot;success.jsp&quot;</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h2 id="2-2-入门案例工作流程分析"><a href="#2-2-入门案例工作流程分析" class="headerlink" title="2.2 入门案例工作流程分析"></a>2.2 入门案例工作流程分析</h2><ul>
<li>服务器启动<ol>
<li>加载web.xml中DispatcherServlet</li>
<li>读取spring-mvc.xml中的配置，加载所有com.itheima包中所有标记为bean的类</li>
<li>读取bean中方法上方标注@RequestMapping的内容</li>
</ol>
</li>
<li>处理请求<ol>
<li>DispatcherServlet配置拦截所有请求 &#x2F;</li>
<li>使用请求路径与所有加载的@RequestMapping的内容进行比对</li>
<li>执行对应的方法</li>
<li>根据方法的返回值在webapp目录中查找对应的页面并展示</li>
</ol>
</li>
</ul>
<h2 id="2-3-SpringMVC-技术架构图"><a href="#2-3-SpringMVC-技术架构图" class="headerlink" title="2.3 SpringMVC 技术架构图"></a>2.3 SpringMVC 技术架构图</h2><p><img src="/myl-mm/day01.assets/image-20200426182959446.png" alt="image-20200426182959446"></p>
<p><img src="/myl-mm/day01.assets/image-20200426183024690.png" alt="image-20200426183024690"></p>
<ul>
<li>DispatcherServlet：前端控制器， 是整体流程控制的中心，由其调用其它组件处理用户的请求， 有<br>  效的降低了组件间的耦合性</li>
<li>HandlerMapping：处理器映射器， 负责根据用户请求找到对应具体的Handler处理器</li>
<li>Handler：处理器，业务处理的核心类，通常由开发者编写，描述具体的业务</li>
<li>HandlAdapter：处理器适配器，通过它对处理器进行执行</li>
<li>View Resolver：视图解析器， 将处理结果生成View视图</li>
<li>View：视图，最终产出结果， 常用视图如jsp、 html</li>
</ul>
<p><img src="/myl-mm/day01.assets/image-20200426183313406.png" alt="image-20200426183313406"></p>
<h1 id="3-基本配置"><a href="#3-基本配置" class="headerlink" title="3 基本配置"></a>3 基本配置</h1><h2 id="3-1-常规配置（Controller加载控制）"><a href="#3-1-常规配置（Controller加载控制）" class="headerlink" title="3.1 常规配置（Controller加载控制）"></a>3.1 常规配置（Controller加载控制）</h2><ul>
<li>SpringMVC的处理器对应的bean必须按照规范格式开发，未避免加入无效的bean可通过bean加载过滤器进<br>行包含设定或排除设定，表现层bean标注通常设定为@Controller</li>
</ul>
<p><strong>xml方式</strong></p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">context:component-scan</span> <span class="attr">base-package</span>=<span class="string">&quot;com.itheima&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">context:include-filter</span></span></span><br><span class="line"><span class="tag">                            <span class="attr">type</span>=<span class="string">&quot;annotation&quot;</span></span></span><br><span class="line"><span class="tag">                            <span class="attr">expression</span>=<span class="string">&quot;org.springframework.stereotype.Controller&quot;</span>/&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">context:component-scan</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h3 id="3-1-1-静态资源加载"><a href="#3-1-1-静态资源加载" class="headerlink" title="3.1.1 静态资源加载"></a>3.1.1 静态资源加载</h3><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--放行指定类型静态资源配置方式--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">mvc:resources</span> <span class="attr">mapping</span>=<span class="string">&quot;/img/**&quot;</span> <span class="attr">location</span>=<span class="string">&quot;/img/&quot;</span>/&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">mvc:resources</span> <span class="attr">mapping</span>=<span class="string">&quot;/js/**&quot;</span> <span class="attr">location</span>=<span class="string">&quot;/js/&quot;</span>/&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">mvc:resources</span> <span class="attr">mapping</span>=<span class="string">&quot;/css/**&quot;</span> <span class="attr">location</span>=<span class="string">&quot;/css/&quot;</span>/&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">&lt;!--SpringMVC提供的通用资源放行方式--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">mvc:default-servlet-handler</span>/&gt;</span></span><br></pre></td></tr></table></figure>

<h3 id="3-1-2-中文乱码处理"><a href="#3-1-2-中文乱码处理" class="headerlink" title="3.1.2 中文乱码处理"></a>3.1.2 中文乱码处理</h3><p>  SpringMVC提供专用的中文字符过滤器，用于处理乱码问题  </p>
<p>  配置在 <strong>web.xml</strong> 里面</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--乱码处理过滤器，与Servlet中使用的完全相同，差异之处在于处理器的类由Spring提供--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">filter</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">filter-name</span>&gt;</span>CharacterEncodingFilter<span class="tag">&lt;/<span class="name">filter-name</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">filter-class</span>&gt;</span>org.springframework.web.filter.CharacterEncodingFilter<span class="tag">&lt;/<span class="name">filter-class</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">init-param</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">param-name</span>&gt;</span>encoding<span class="tag">&lt;/<span class="name">param-name</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">param-value</span>&gt;</span>UTF-8<span class="tag">&lt;/<span class="name">param-value</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">init-param</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">filter</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">filter-mapping</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">filter-name</span>&gt;</span>CharacterEncodingFilter<span class="tag">&lt;/<span class="name">filter-name</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">url-pattern</span>&gt;</span>/*<span class="tag">&lt;/<span class="name">url-pattern</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">filter-mapping</span>&gt;</span></span><br></pre></td></tr></table></figure>



<h2 id="3-2-注解驱动"><a href="#3-2-注解驱动" class="headerlink" title="3.2 注解驱动"></a>3.2 注解驱动</h2><ul>
<li>使用注解形式转化SpringMVC核心配置文件为配置类</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Configuration</span></span><br><span class="line"><span class="meta">@ComponentScan(value = &quot;com.itheima&quot;,includeFilters =</span></span><br><span class="line"><span class="meta">    @ComponentScan.Filter(type=FilterType.ANNOTATION,classes = &#123;Controller.class&#125;)</span></span><br><span class="line"><span class="meta">    )</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">SpringMVCConfiguration</span> <span class="keyword">implements</span> <span class="title class_">WebMvcConfigurer</span>&#123;</span><br><span class="line">    <span class="comment">//注解配置放行指定资源格式</span></span><br><span class="line"><span class="comment">//    @Override</span></span><br><span class="line"><span class="comment">//    public void addResourceHandlers(ResourceHandlerRegistry registry) &#123;</span></span><br><span class="line"><span class="comment">//        registry.addResourceHandler(&quot;/img/**&quot;).addResourceLocations(&quot;/img/&quot;);</span></span><br><span class="line"><span class="comment">//        registry.addResourceHandler(&quot;/js/**&quot;).addResourceLocations(&quot;/js/&quot;);</span></span><br><span class="line"><span class="comment">//        registry.addResourceHandler(&quot;/css/**&quot;).addResourceLocations(&quot;/css/&quot;);</span></span><br><span class="line"><span class="comment">//    &#125;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">//注解配置通用放行资源的格式</span></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">configureDefaultServletHandling</span><span class="params">(DefaultServletHandlerConfigurer configurer)</span> &#123;</span><br><span class="line">        configurer.enable();;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>基于servlet3.0规范，自定义Servlet容器初始化配置类，加载SpringMVC核心配置类</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">ServletContainersInitConfig</span> <span class="keyword">extends</span> <span class="title class_">AbstractDispatcherServletInitializer</span> &#123;</span><br><span class="line">    <span class="comment">//创建Servlet容器时，使用注解的方式加载SPRINGMVC配置类中的信息，并加载成WEB专用的			           //ApplicationContext对象</span></span><br><span class="line">    <span class="comment">//该对象放入了ServletContext范围，后期在整个WEB容器中可以随时获取调用</span></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">protected</span> WebApplicationContext <span class="title function_">createServletApplicationContext</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="type">AnnotationConfigWebApplicationContext</span> <span class="variable">ctx</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">AnnotationConfigWebApplicationContext</span>();</span><br><span class="line">        ctx.register(SpringMVCConfiguration.class);</span><br><span class="line">        <span class="keyword">return</span> ctx;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//注解配置映射地址方式，服务于SpringMVC的核心控制器DispatcherServlet</span></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">protected</span> String[] getServletMappings() &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> <span class="title class_">String</span>[]&#123;<span class="string">&quot;/&quot;</span>&#125;;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">protected</span> WebApplicationContext <span class="title function_">createRootApplicationContext</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">null</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//乱码处理作为过滤器，在servlet容器启动时进行配置，相关内容参看Servlet零配置相关课程</span></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">onStartup</span><span class="params">(ServletContext servletContext)</span> <span class="keyword">throws</span> ServletException &#123;</span><br><span class="line">        <span class="built_in">super</span>.onStartup(servletContext);</span><br><span class="line">        <span class="type">CharacterEncodingFilter</span> <span class="variable">cef</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">CharacterEncodingFilter</span>();</span><br><span class="line">        cef.setEncoding(<span class="string">&quot;UTF-8&quot;</span>);</span><br><span class="line">        FilterRegistration.<span class="type">Dynamic</span> <span class="variable">registration</span> <span class="operator">=</span> servletContext.addFilter(<span class="string">&quot;characterEncodingFilter&quot;</span>, cef);</span><br><span class="line">        registration.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST,DispatcherType.FORWARD,DispatcherType.INCLUDE),<span class="literal">false</span>,<span class="string">&quot;/*&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>  删除web.xml<br>  删除spring-mvc.xml  </p>
<p> <strong>小节</strong><br> 基于servlet3.0规范，配置Servlet容器初始化配置类，初始化时加载SpringMVC配置类<br> 转化SpringMVC核心配置文件<br> 转化为注解（例如： spring处理器加载过滤）<br> 转化为bean进行加载<br> 按照标准接口进行开发并加载（例如：中文乱码处理、静态资源加载过滤）  </p>
<h1 id="4-请求"><a href="#4-请求" class="headerlink" title="4 请求"></a>4 请求</h1><h2 id="4-1-普通类型参数传参"><a href="#4-1-普通类型参数传参" class="headerlink" title="4.1 普通类型参数传参"></a>4.1 普通类型参数传参</h2><p>  参数名与处理器方法形参名保持一致  </p>
<p>  访问URL： <a target="_blank" rel="noopener" href="http://localhost/requestParam1?name=itheima&amp;age=14">http://localhost/requestParam1?name=itheima&amp;age=14</a>  </p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RequestMapping(&quot;/requestParam1&quot;)</span></span><br><span class="line"><span class="keyword">public</span> String <span class="title function_">requestParam1</span><span class="params">(String name ,String age)</span>&#123;</span><br><span class="line">    System.out.println(<span class="string">&quot;name=&quot;</span>+name+<span class="string">&quot;,age=&quot;</span>+age);</span><br><span class="line">    <span class="keyword">return</span> <span class="string">&quot;page.jsp&quot;</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p> <strong>@RequestParam</strong> 的使用<br> 类型： 形参注解<br> 位置：处理器类中的方法形参前方<br> 作用：绑定请求参数与对应处理方法形参间的关系  </p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RequestMapping(&quot;/requestParam2&quot;)</span></span><br><span class="line"><span class="keyword">public</span> String <span class="title function_">requestParam2</span><span class="params">(<span class="meta">@RequestParam(</span></span></span><br><span class="line"><span class="meta"><span class="params">                            name = &quot;userName&quot;,</span></span></span><br><span class="line"><span class="meta"><span class="params">                            required = true,</span></span></span><br><span class="line"><span class="meta"><span class="params">                            defaultValue = &quot;itheima&quot;)</span> String name)</span>&#123;</span><br><span class="line">    </span><br><span class="line">    System.out.println(<span class="string">&quot;name=&quot;</span>+name);</span><br><span class="line">    <span class="keyword">return</span> <span class="string">&quot;page.jsp&quot;</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="4-2-POJO类型参数传参"><a href="#4-2-POJO类型参数传参" class="headerlink" title="4.2 POJO类型参数传参"></a>4.2 POJO类型参数传参</h2><p>当POJO中使用简单类型属性时， 参数名称与POJO类属性名保持一致  </p>
<p>访问URL： <a target="_blank" rel="noopener" href="http://localhost/requestParam3?name=itheima&amp;age=14">http://localhost/requestParam3?name=itheima&amp;age=14</a>  </p>
<p><strong>Controller</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RequestMapping(&quot;/requestParam3&quot;)</span></span><br><span class="line"><span class="keyword">public</span> String <span class="title function_">requestParam3</span><span class="params">(User user)</span>&#123;</span><br><span class="line">    System.out.println(<span class="string">&quot;name=&quot;</span>+user.getName());</span><br><span class="line">    <span class="keyword">return</span> <span class="string">&quot;page.jsp&quot;</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><strong>POJO类</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">User</span> &#123;</span><br><span class="line">    <span class="keyword">private</span> String name;</span><br><span class="line">    <span class="keyword">private</span> Integer age;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">public</span> String <span class="title function_">getName</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setName</span><span class="params">(String name)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.name = name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> Integer <span class="title function_">getAge</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setAge</span><span class="params">(Integer age)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.age = age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<p><strong>参数冲突</strong><br> 当POJO类型属性与其他形参出现同名问题时，将被同时赋值<br> 建议使用@RequestParam注解进行区分<br>访问URL： <a target="_blank" rel="noopener" href="http://localhost/requestParam4?name=itheima&amp;**age**=14">http://localhost/requestParam4?name=itheima&amp;**age**=14</a>  </p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RequestMapping(&quot;/requestParam4&quot;)</span></span><br><span class="line"><span class="keyword">public</span> String <span class="title function_">requestParam4</span><span class="params">(User user,String age)</span>&#123;</span><br><span class="line">    System.out.println(<span class="string">&quot;user.age=&quot;</span>+user.getAge()+<span class="string">&quot;,age=&quot;</span>+age);</span><br><span class="line">    <span class="keyword">return</span> <span class="string">&quot;page.jsp&quot;</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<p><strong>复杂POJO类型参数</strong><br> 当POJO中出现对象属性时，参数名称与对象层次结构名称保持一致  </p>
<p>  访问URL： <a target="_blank" rel="noopener" href="http://localhost/requestParam5?address.province=beijing">http://localhost/requestParam5?address.province=beijing</a>  </p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">User</span> &#123;</span><br><span class="line">    <span class="keyword">private</span> String name;</span><br><span class="line">    <span class="keyword">private</span> Integer age;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> Address address;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">public</span> Address <span class="title function_">getAddress</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> address;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setAddress</span><span class="params">(Address address)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.address = address;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> String <span class="title function_">getName</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setName</span><span class="params">(String name)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.name = name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> Integer <span class="title function_">getAge</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setAge</span><span class="params">(Integer age)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.age = age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RequestMapping(&quot;/requestParam5&quot;)</span></span><br><span class="line"><span class="keyword">public</span> String <span class="title function_">requestParam5</span><span class="params">(User user)</span>&#123;</span><br><span class="line">    System.out.println(<span class="string">&quot;user.address=&quot;</span>+user.getAddress().getProvince());</span><br><span class="line">    <span class="keyword">return</span> <span class="string">&quot;page.jsp&quot;</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><img src="/myl-mm/day01.assets/image-20200426190932459.png" alt="image-20200426190932459"></p>
<p><strong>当POJO中出现List，保存对象数据，参数名称与对象层次结构名称保持一致，使用数组格式描述集合中对象的位置</strong>  </p>
<p>访问URL： <a target="_blank" rel="noopener" href="http://localhost/requestParam7?addresses%5B0%5D.province=bj&amp;addresses%5B1%5D.province=tj">http://localhost/requestParam7?addresses[0].province=bj&amp;addresses[1].province=tj</a>  </p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">User</span> &#123;</span><br><span class="line">    <span class="keyword">private</span> String name;</span><br><span class="line">    <span class="keyword">private</span> Integer age;</span><br><span class="line">    <span class="keyword">private</span> List&lt;Address&gt; addresses;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Address</span> &#123;</span><br><span class="line">    <span class="keyword">private</span> String province;</span><br><span class="line">    <span class="keyword">private</span> String city;</span><br><span class="line">    <span class="keyword">private</span> String address;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RequestMapping(&quot;/requestParam7&quot;)</span></span><br><span class="line"><span class="keyword">public</span> String <span class="title function_">requestParam7</span><span class="params">(User user)</span>&#123;</span><br><span class="line">    System.out.println(<span class="string">&quot;user.addresses=&quot;</span>+user.getAddress());</span><br><span class="line">    <span class="keyword">return</span> <span class="string">&quot;page.jsp&quot;</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p> <strong>当POJO中出现Map，保存对象数据，参数名称与对象层次结构名称保持一致，使用映射格式描述集合中对象的位置</strong>  </p>
<p> 访问URL： <a target="_blank" rel="noopener" href="http://localhost/requestParam8?addressMap%5B%E2%80%99home%E2%80%99%5D.province=bj&amp;addressMap%5B%E2%80%99job%E2%80%99%5D.province=tj">http://localhost/requestParam8?addressMap[’home’].province=bj&amp;addressMap[’job’].province=tj</a>  </p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">User</span> &#123;</span><br><span class="line">    <span class="keyword">private</span> String name;</span><br><span class="line">    <span class="keyword">private</span> Integer age;</span><br><span class="line">    <span class="keyword">private</span> Map&lt;String,Address&gt; addressMap;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Address</span> &#123;</span><br><span class="line">    <span class="keyword">private</span> String province;</span><br><span class="line">    <span class="keyword">private</span> String city;</span><br><span class="line">    <span class="keyword">private</span> String address;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RequestMapping(&quot;/requestParam8&quot;)</span></span><br><span class="line"><span class="keyword">public</span> String <span class="title function_">requestParam8</span><span class="params">(User user)</span>&#123;</span><br><span class="line">    System.out.println(<span class="string">&quot;user.addressMap=&quot;</span>+user.getAddressMap());</span><br><span class="line">    <span class="keyword">return</span> <span class="string">&quot;page.jsp&quot;</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="4-3-数组与集合类型参数传参"><a href="#4-3-数组与集合类型参数传参" class="headerlink" title="4.3 数组与集合类型参数传参"></a>4.3 数组与集合类型参数传参</h2><p><strong>数组类型参数</strong></p>
<p>请求参数名与处理器方法形参名保持一致，且请求参数数量＞ 1个  </p>
<p>访问URL： <a target="_blank" rel="noopener" href="http://localhost/requestParam9?nick=Jockme&amp;nick=zahc">http://localhost/requestParam9?nick=Jockme&amp;nick=zahc</a>  </p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RequestMapping(&quot;/requestParam9&quot;)</span></span><br><span class="line"><span class="keyword">public</span> String <span class="title function_">requestParam9</span><span class="params">(String[] nick)</span>&#123;</span><br><span class="line">    System.out.println(nick[<span class="number">0</span>]+<span class="string">&quot;,&quot;</span>+nick[<span class="number">1</span>]);</span><br><span class="line">    <span class="keyword">return</span> <span class="string">&quot;page.jsp&quot;</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><strong>集合类型参数</strong><br> 保存简单类型数据，请求参数名与处理器方法形参名保持一致，且请求参数数量＞ 1个<br>访问URL： <a target="_blank" rel="noopener" href="http://localhost/requestParam10?nick=Jockme&amp;nick=zahc">http://localhost/requestParam10?nick=Jockme&amp;nick=zahc</a></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RequestMapping(&quot;/requestParam10&quot;)</span></span><br><span class="line"><span class="keyword">public</span> String <span class="title function_">requestParam10</span><span class="params">(<span class="meta">@RequestParam(&quot;nick&quot;)</span> List&lt;String&gt; nick)</span>&#123;</span><br><span class="line">    System.out.println(nick);</span><br><span class="line">    <span class="keyword">return</span> <span class="string">&quot;page.jsp&quot;</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p> 注意： SpringMVC默认将List作为对象处理，赋值前先创建对象，然后将nick作为对象的属性进行处理。由于<br>List是接口，无法创建对象，报无法找到构造方法异常；修复类型为可创建对象的ArrayList类型后，对象可<br>以创建，但没有nick属性，因此数据为空。此时需要告知SpringMVC的处理器nick是一组数据，而不是一个单<br>一数据。通过@RequestParam注解，将数量大于1个names参数打包成参数数组后， SpringMVC才能识别该数<br>据格式，并判定形参类型是否为数组或集合，并按数组或集合对象的形式操作数据。  </p>
<p> <strong>小节</strong><br> 请求POJO类型参数获取<br> POJO的简单属性<br> POJO的对象属性<br> POJO的集合属性（存储简单数据）<br> POJO的集合属性（存储对象数据）<br> 名称冲突问题  </p>
<h2 id="4-4-类型转换器"><a href="#4-4-类型转换器" class="headerlink" title="4.4 类型转换器"></a>4.4 类型转换器</h2><p>SpringMVC对接收的数据进行自动类型转换，该工作通过Converter接口实现  </p>
<p><img src="/myl-mm/day01.assets/image-20200426193801059.png" alt="image-20200426193801059"></p>
<ul>
<li><p><strong>标量转换器</strong><br> StringToBooleanConverter String→Boolean<br> ObjectToStringConverter Object→String<br> StringToNumberConverterFactory String→Number（ Integer、 Long等）<br> NumberToNumberConverterFactory Number子类型之间(Integer、 Long、 Double等)<br> StringToCharacterConverter String→java.lang.Character<br> NumberToCharacterConverter Number子类型(Integer、 Long、 Double等)→java.lang.Character<br> CharacterToNumberFactory java.lang.Character→Number子类型(Integer、 Long、 Double等)<br> StringToEnumConverterFactory String→enum类型<br> EnumToStringConverter enum类型→String<br> StringToLocaleConverter String→java.util.Local<br> PropertiesToStringConverter java.util.Properties→String<br> StringToPropertiesConverter String→java.util.Properties  </p>
</li>
<li><p><strong>集合、数组相关转换器</strong><br> ArrayToCollectionConverter 数组→集合（ List、 Set）<br> CollectionToArrayConverter 集合（ List、 Set） →数组<br> ArrayToArrayConverter 数组间<br> CollectionToCollectionConverter 集合间（ List、 Set）<br> MapToMapConverter Map间<br> ArrayToStringConverter 数组→String类型<br> StringToArrayConverter String→数组， trim后使用“,”split<br> ArrayToObjectConverter 数组→Object<br> ObjectToArrayConverter Object→单元素数组<br> CollectionToStringConverter 集合（ List、 Set） →String<br> StringToCollectionConverter String→集合（ List、 Set）， trim后使用“,”split<br> CollectionToObjectConverter 集合→Object<br> ObjectToCollectionConverter Object→单元素集合  </p>
</li>
<li><p><strong>默认转换器</strong><br>   ObjectToObjectConverter Object间<br>   IdToEntityConverter Id→Entity<br>   FallbackObjectToStringConverter Object→String  </p>
</li>
<li><p><strong>SpringMVC对接收的数据进行自动类型转换，该工作通过Converter接口实现</strong>  </p>
<p><img src="/myl-mm/day01.assets/image-20200426194106166.png" alt="image-20200426194106166"></p>
</li>
</ul>
<h2 id="4-5-日期类型格式转换"><a href="#4-5-日期类型格式转换" class="headerlink" title="4.5 日期类型格式转换"></a>4.5 日期类型格式转换</h2><ul>
<li><p><strong>声明自定义的转换格式并覆盖系统转换格式</strong>  </p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--5.启用自定义Converter--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">mvc:annotation-driven</span> <span class="attr">conversion-service</span>=<span class="string">&quot;conversionService&quot;</span>/&gt;</span></span><br><span class="line"><span class="comment">&lt;!--1.设定格式类型Converter，注册为Bean，受SpringMVC管理--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">bean</span> <span class="attr">id</span>=<span class="string">&quot;conversionService&quot;</span></span></span><br><span class="line"><span class="tag">      <span class="attr">class</span>=<span class="string">&quot;org.springframework.format.support.FormattingConversionServiceFactoryBean&quot;</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--2.自定义Converter格式类型设定，该设定使用的是同类型覆盖的思想--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;formatters&quot;</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!--3.使用set保障相同类型的转换器仅保留一个，避免冲突--&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">set</span>&gt;</span></span><br><span class="line">            <span class="comment">&lt;!--4.设置具体的格式类型--&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">bean</span> <span class="attr">class</span>=<span class="string">&quot;org.springframework.format.datetime.DateFormatter&quot;</span>&gt;</span></span><br><span class="line">                <span class="comment">&lt;!--5.类型规则--&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;pattern&quot;</span> <span class="attr">value</span>=<span class="string">&quot;yyyy-MM-dd&quot;</span>/&gt;</span></span><br><span class="line">            <span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">set</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">property</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>日期类型格式转换（简化版）</strong><br> 名称： @DateTimeFormat<br> 类型： 形参注解、成员变量注解<br> 位置：形参前面 或 成员变量上方<br> 作用：为当前参数或变量指定类型转换规则<br> 范例：  </p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> String <span class="title function_">requestParam12</span><span class="params">(<span class="meta">@DateTimeFormat(pattern = &quot;yyyy-MM-dd&quot;)</span> Date date)</span>&#123;</span><br><span class="line">    System.out.println(<span class="string">&quot;date=&quot;</span>+date);</span><br><span class="line">    <span class="keyword">return</span> <span class="string">&quot;page.jsp&quot;</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@DateTimeFormat(pattern = &quot;yyyy-MM-dd&quot;)</span></span><br><span class="line"><span class="keyword">private</span> Date birthday;</span><br></pre></td></tr></table></figure>
</li>
<li><p>注意：依赖注解驱动支持  </p>
<p>  &lt;mvc:annotation-driven &#x2F;&gt;</p>
</li>
</ul>
<h2 id="4-6-自定义类型转换器"><a href="#4-6-自定义类型转换器" class="headerlink" title="4.6 自定义类型转换器"></a>4.6 自定义类型转换器</h2><ul>
<li><p>自定义类型转换器，实现Converter接口，并制定转换前与转换后的类型</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--1.将自定义Converter注册为Bean，受SpringMVC管理--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">bean</span> <span class="attr">id</span>=<span class="string">&quot;myDateConverter&quot;</span> <span class="attr">class</span>=<span class="string">&quot;com.itheima.converter.MyDateConverter&quot;</span>/&gt;</span></span><br><span class="line"><span class="comment">&lt;!--2.设定自定义Converter服务bean--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">bean</span> <span class="attr">id</span>=<span class="string">&quot;conversionService&quot;</span></span></span><br><span class="line"><span class="tag">      <span class="attr">class</span>=<span class="string">&quot;org.springframework.context.support.ConversionServiceFactoryBean&quot;</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--3.注入所有的自定义Converter，该设定使用的是同类型覆盖的思想--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;converters&quot;</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!--4.set保障同类型转换器仅保留一个，去重规则以Converter&lt;S,T&gt;的泛型为准--&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">set</span>&gt;</span></span><br><span class="line">            <span class="comment">&lt;!--5.具体的类型转换器--&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">ref</span> <span class="attr">bean</span>=<span class="string">&quot;myDateConverter&quot;</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">set</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">property</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//自定义类型转换器，实现Converter接口，接口中指定的泛型即为最终作用的条件</span></span><br><span class="line"><span class="comment">//本例中的泛型填写的是String，Date，最终出现字符串转日期时，该类型转换器生效</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">MyDateConverter</span> <span class="keyword">implements</span> <span class="title class_">Converter</span>&lt;String, Date&gt; &#123;</span><br><span class="line">    <span class="comment">//重写接口的抽象方法，参数由泛型决定</span></span><br><span class="line">    <span class="keyword">public</span> Date <span class="title function_">convert</span><span class="params">(String source)</span> &#123;</span><br><span class="line">        <span class="type">DateFormat</span> <span class="variable">df</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">SimpleDateFormat</span>(<span class="string">&quot;yyyy-MM-dd&quot;</span>);</span><br><span class="line">        <span class="type">Date</span> <span class="variable">date</span> <span class="operator">=</span> <span class="literal">null</span>;</span><br><span class="line">        <span class="comment">//类型转换器无法预计使用过程中出现的异常，因此必须在类型转换器内部捕获，不允许抛出，框架无法预计此类异常如何处理</span></span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            date = df.parse(source);</span><br><span class="line">        &#125; <span class="keyword">catch</span> (ParseException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> date;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


</li>
<li><p><strong>通过注册自定义转换器，将该功能加入到SpringMVC的转换服务ConverterService中</strong>  </p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--开启注解驱动，加载自定义格式化转换器对应的类型转换服务--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">mvc:annotation-driven</span> <span class="attr">conversion-service</span>=<span class="string">&quot;conversionService&quot;</span>/&gt;</span></span><br></pre></td></tr></table></figure></li>
</ul>
<h2 id="4-7-请求映射-RequestMapping"><a href="#4-7-请求映射-RequestMapping" class="headerlink" title="4.7 请求映射 @RequestMapping"></a>4.7 请求映射 @RequestMapping</h2><h3 id="4-7-1-方法注解"><a href="#4-7-1-方法注解" class="headerlink" title="4.7.1 方法注解"></a>4.7.1 方法注解</h3><ul>
<li>名称： @RequestMapping<br> 类型： 方法注解<br> 位置：处理器类中的方法定义上方<br> 作用：绑定请求地址与对应处理方法间的关系<br> 范例：<br> 访问路径： &#x2F;requestURL1</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Controller</span></span><br><span class="line"><span class="meta">@RequestMapping(&quot;/user&quot;)</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">UserController</span> &#123;</span><br><span class="line">    <span class="meta">@RequestMapping(&quot;/requestURL2&quot;)</span></span><br><span class="line">    <span class="keyword">public</span> String <span class="title function_">requestURL2</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">&quot;page.jsp&quot;</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="4-7-2-类注解"><a href="#4-7-2-类注解" class="headerlink" title="4.7.2 类注解"></a>4.7.2 类注解</h3><p><strong>名称： @RequestMapping</strong><br> 类型： 类注解<br> 位置：处理器类定义上方<br> 作用：为当前处理器中所有方法设定公共的访问路径前缀<br> 范例：<br> 访问路径： &#x2F;user&#x2F;requestURL1</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Controller</span></span><br><span class="line"><span class="meta">@RequestMapping(&quot;/user&quot;)</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">UserController</span> &#123;</span><br><span class="line">    <span class="meta">@RequestMapping(&quot;/requestURL2&quot;)</span></span><br><span class="line">    <span class="keyword">public</span> String <span class="title function_">requestURL2</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">&quot;page.jsp&quot;</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;  </span><br></pre></td></tr></table></figure>

<ul>
<li><p>常用属性</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RequestMapping(</span></span><br><span class="line"><span class="meta">    value=&quot;/requestURL3&quot;, //设定请求路径，与path属性、 value属性相同</span></span><br><span class="line"><span class="meta">    method = RequestMethod.GET, //设定请求方式</span></span><br><span class="line"><span class="meta">    params = &quot;name&quot;, //设定请求参数条件</span></span><br><span class="line"><span class="meta">    headers = &quot;content-type=text/*&quot;, //设定请求消息头条件</span></span><br><span class="line"><span class="meta">    consumes = &quot;text/*&quot;, //用于指定可以接收的请求正文类型（MIME类型）</span></span><br><span class="line"><span class="meta">    produces = &quot;text/*&quot; //用于指定可以生成的响应正文类型（MIME类型）</span></span><br><span class="line"><span class="meta">)</span></span><br><span class="line"><span class="keyword">public</span> String <span class="title function_">requestURL3</span><span class="params">()</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="string">&quot;/page.jsp&quot;</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
</ul>
<h1 id="5-响应"><a href="#5-响应" class="headerlink" title="5 响应"></a>5 响应</h1><h2 id="5-1-页面跳转"><a href="#5-1-页面跳转" class="headerlink" title="5.1 页面跳转"></a>5.1 页面跳转</h2><ul>
<li>转发（默认）</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RequestMapping(&quot;/showPage1&quot;)</span></span><br><span class="line"><span class="keyword">public</span> String <span class="title function_">showPage1</span><span class="params">()</span> &#123;</span><br><span class="line">    System.out.println(<span class="string">&quot;user mvc controller is running ...&quot;</span>);</span><br><span class="line">    <span class="keyword">return</span> <span class="string">&quot;forward:page.jsp&quot;</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>重定向</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">@RequestMapping(&quot;/showPage2&quot;)</span><br><span class="line">public String showPage2() &#123;</span><br><span class="line">System.out.println(&quot;user mvc controller is running ...&quot;);</span><br><span class="line">return &quot;redirect:page.jsp&quot;;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p> 注意：页面访问地址中所携带的   <strong>&#x2F;</strong>  </p>
<h2 id="5-2-页面访问快捷设定-InternalResourceViewResolver"><a href="#5-2-页面访问快捷设定-InternalResourceViewResolver" class="headerlink" title="5.2 页面访问快捷设定 (InternalResourceViewResolver)"></a>5.2 页面访问快捷设定 (InternalResourceViewResolver)</h2><p>  展示页面的保存位置通常固定，且结构相似，可以设定通用的访问路径，简化页面配置格式  </p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&lt;bean class=<span class="string">&quot;org.springframework.web.servlet.view.InternalResourceViewResolver&quot;</span>&gt;</span><br><span class="line">    &lt;property name=<span class="string">&quot;prefix&quot;</span> value=<span class="string">&quot;/WEB-INF/pages/&quot;</span>/&gt;</span><br><span class="line">    &lt;property name=<span class="string">&quot;suffix&quot;</span> value=<span class="string">&quot;.jsp&quot;</span>/&gt;</span><br><span class="line">/bean&gt;</span><br></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> String <span class="title function_">showPage3</span><span class="params">()</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="string">&quot;page&quot;</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>  <strong>如果未设定了返回值，使用void类型，则默认使用访问路径作页面地址的前缀后缀</strong>  </p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//最简页面配置方式，使用访问路径作为页面名称，省略返回值</span></span><br><span class="line"><span class="meta">@RequestMapping(&quot;/showPage5&quot;)</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">showPage5</span><span class="params">()</span> &#123;</span><br><span class="line">    System.out.println(<span class="string">&quot;user mvc controller is running ...&quot;</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="5-3-带数据页面跳转"><a href="#5-3-带数据页面跳转" class="headerlink" title="5.3 带数据页面跳转"></a>5.3 带数据页面跳转</h2><ul>
<li>方式一：使用HttpServletRequest类型形参进行数据传递</li>
</ul>
  <figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RequestMapping(&quot;/showPageAndData1&quot;)</span></span><br><span class="line"><span class="keyword">public</span> String <span class="title function_">showPageAndData1</span><span class="params">(HttpServletRequest request)</span> &#123;</span><br><span class="line">    request.setAttribute(<span class="string">&quot;name&quot;</span>,<span class="string">&quot;itheima&quot;</span>);</span><br><span class="line">    <span class="keyword">return</span> <span class="string">&quot;page&quot;</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>方式二：使用Model类型形参进行数据传递</li>
</ul>
  <figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RequestMapping(&quot;/showPageAndData2&quot;)</span></span><br><span class="line"><span class="keyword">public</span> String <span class="title function_">showPageAndData2</span><span class="params">(Model model)</span> &#123;</span><br><span class="line">    model.addAttribute(<span class="string">&quot;name&quot;</span>,<span class="string">&quot;itheima&quot;</span>);</span><br><span class="line">    <span class="type">Book</span> <span class="variable">book</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Book</span>();</span><br><span class="line">    book.setName(<span class="string">&quot;SpringMVC入门实战&quot;</span>);</span><br><span class="line">    book.setPrice(<span class="number">66.6d</span>);</span><br><span class="line">    model.addAttribute(<span class="string">&quot;book&quot;</span>,book);</span><br><span class="line">    <span class="keyword">return</span> <span class="string">&quot;page&quot;</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>方式三：使用ModelAndView类型形参进行数据传递，将该对象作为返回值传递给调用者</li>
</ul>
  <figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//使用ModelAndView形参传递参数，该对象还封装了页面信息</span></span><br><span class="line"><span class="meta">@RequestMapping(&quot;/showPageAndData3&quot;)</span></span><br><span class="line"><span class="keyword">public</span> ModelAndView <span class="title function_">showPageAndData3</span><span class="params">(ModelAndView modelAndView)</span> &#123;</span><br><span class="line">    <span class="comment">//ModelAndView mav = new ModelAndView();    替换形参中的参数</span></span><br><span class="line">    <span class="type">Book</span> <span class="variable">book</span>  <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Book</span>();</span><br><span class="line">    book.setName(<span class="string">&quot;SpringMVC入门案例&quot;</span>);</span><br><span class="line">    book.setPrice(<span class="number">66.66d</span>);</span><br><span class="line">    <span class="comment">//添加数据的方式，key对value</span></span><br><span class="line">    modelAndView.addObject(<span class="string">&quot;book&quot;</span>,book);</span><br><span class="line">    <span class="comment">//添加数据的方式，key对value</span></span><br><span class="line">    modelAndView.addObject(<span class="string">&quot;name&quot;</span>,<span class="string">&quot;Jockme&quot;</span>);</span><br><span class="line">    <span class="comment">//设置页面的方式，该方法最后一次执行的结果生效</span></span><br><span class="line">    modelAndView.setViewName(<span class="string">&quot;page&quot;</span>);</span><br><span class="line">    <span class="comment">//返回值设定成ModelAndView对象</span></span><br><span class="line">    <span class="keyword">return</span> modelAndView;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="5-4-返回json数据"><a href="#5-4-返回json数据" class="headerlink" title="5.4 返回json数据"></a>5.4 返回json数据</h2><ul>
<li>方式一：基于response返回数据的简化格式，返回JSON数据</li>
</ul>
  <figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//使用jackson进行json数据格式转化</span></span><br><span class="line"><span class="meta">@RequestMapping(&quot;/showData3&quot;)</span></span><br><span class="line"><span class="meta">@ResponseBody</span></span><br><span class="line"><span class="keyword">public</span> String <span class="title function_">showData3</span><span class="params">()</span> <span class="keyword">throws</span> JsonProcessingException &#123;</span><br><span class="line">    <span class="type">Book</span> <span class="variable">book</span>  <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Book</span>();</span><br><span class="line">    book.setName(<span class="string">&quot;SpringMVC入门案例&quot;</span>);</span><br><span class="line">    book.setPrice(<span class="number">66.66d</span>);</span><br><span class="line"></span><br><span class="line">    <span class="type">ObjectMapper</span> <span class="variable">om</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">ObjectMapper</span>();</span><br><span class="line">    <span class="keyword">return</span> om.writeValueAsString(book);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>使用SpringMVC提供的消息类型转换器将对象与集合数据自动转换为JSON数据</li>
</ul>
  <figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//使用SpringMVC注解驱动，对标注@ResponseBody注解的控制器方法进行结果转换，由于返回值为引用类型，自动调用jackson提供的类型转换器进行格式转换</span></span><br><span class="line"><span class="meta">@RequestMapping(&quot;/showData4&quot;)</span></span><br><span class="line"><span class="meta">@ResponseBody</span></span><br><span class="line"><span class="keyword">public</span> Book <span class="title function_">showData4</span><span class="params">()</span> &#123;</span><br><span class="line">    <span class="type">Book</span> <span class="variable">book</span>  <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Book</span>();</span><br><span class="line">    book.setName(<span class="string">&quot;SpringMVC入门案例&quot;</span>);</span><br><span class="line">    book.setPrice(<span class="number">66.66d</span>);</span><br><span class="line">    <span class="keyword">return</span> book;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<pre><code>需要手工添加信息类型转换器  
</code></pre>
  <figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">bean</span> <span class="attr">class</span>=<span class="string">&quot;org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;messageConverters&quot;</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">list</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">bean</span> <span class="attr">class</span>=<span class="string">&quot;org.springframework.http.converter.json.MappingJackson2HttpMessageConverter&quot;</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">list</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">property</span>&gt;</span></span><br><span class="line">&lt;/bean</span><br></pre></td></tr></table></figure>

<ul>
<li>方式三：使用SpringMVC注解驱动简化配置</li>
</ul>
  <figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--开启springmvc注解驱动，对@ResponseBody的注解进行格式增强，追加其类型转换的功能，具体实现由MappingJackson2HttpMessageConverter进行--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">mvc:annotation-driven</span>/&gt;</span></span><br></pre></td></tr></table></figure>

<h1 id="6-Servlet相关接口-Servlet相关接口替换方案"><a href="#6-Servlet相关接口-Servlet相关接口替换方案" class="headerlink" title="6 Servlet相关接口-Servlet相关接口替换方案"></a>6 Servlet相关接口-Servlet相关接口替换方案</h1><p>  <strong>HttpServletRequest &#x2F; HttpServletResponse &#x2F; HttpSession</strong>  </p>
<ul>
<li><strong>SpringMVC提供访问原始Servlet接口API的功能，通过形参声明即可</strong></li>
</ul>
  <figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RequestMapping(&quot;/servletApi&quot;)</span></span><br><span class="line"><span class="keyword">public</span> String <span class="title function_">servletApi</span><span class="params">(HttpServletRequest request,</span></span><br><span class="line"><span class="params">                         HttpServletResponse response, HttpSession session)</span>&#123;</span><br><span class="line">    System.out.println(request);</span><br><span class="line">    System.out.println(response);</span><br><span class="line">    System.out.println(session);</span><br><span class="line">    request.setAttribute(<span class="string">&quot;name&quot;</span>,<span class="string">&quot;itheima&quot;</span>);</span><br><span class="line">    System.out.println(request.getAttribute(<span class="string">&quot;name&quot;</span>));</span><br><span class="line">    <span class="keyword">return</span> <span class="string">&quot;page.jsp&quot;</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li><strong>Head数据获取</strong></li>
</ul>
<p>   名称： @RequestHeader<br>   类型： 形参注解<br>   位置：处理器类中的方法形参前方<br>   作用：绑定请求头数据与对应处理方法形参间的关系<br>   范例：</p>
  <figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RequestMapping(&quot;/headApi&quot;)</span></span><br><span class="line"><span class="keyword">public</span> String <span class="title function_">headApi</span><span class="params">(<span class="meta">@RequestHeader(&quot;Accept-Language&quot;)</span> String head)</span>&#123;</span><br><span class="line">    System.out.println(head);</span><br><span class="line">    <span class="keyword">return</span> <span class="string">&quot;page.jsp&quot;</span>;</span><br><span class="line">&#125;  </span><br></pre></td></tr></table></figure>

<ul>
<li><strong>Cookie数据获取</strong></li>
</ul>
<p>   名称： @CookieValue<br>   类型： 形参注解<br>   位置：处理器类中的方法形参前方<br>   作用：绑定请求Cookie数据与对应处理方法形参间的关系<br>   范例：</p>
  <figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RequestMapping(&quot;/cookieApi&quot;)</span></span><br><span class="line"><span class="keyword">public</span> String <span class="title function_">cookieApi</span><span class="params">(<span class="meta">@CookieValue(&quot;JSESSIONID&quot;)</span> String jsessionid)</span>&#123;</span><br><span class="line">    System.out.println(jsessionid);</span><br><span class="line">    <span class="keyword">return</span> <span class="string">&quot;page.jsp&quot;</span>;</span><br><span class="line">&#125;  </span><br></pre></td></tr></table></figure>

<ul>
<li><strong>Session数据获取</strong></li>
</ul>
<p>   名称： @SessionAttribute<br>   类型： 形参注解<br>   位置：处理器类中的方法形参前方<br>   作用：绑定请求Session数据与对应处理方法形参间的关系<br>   范例：</p>
  <figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RequestMapping(&quot;/sessionApi&quot;)</span></span><br><span class="line"><span class="keyword">public</span> String <span class="title function_">sessionApi</span><span class="params">(<span class="meta">@SessionAttribute(&quot;name&quot;)</span> String name)</span>&#123;</span><br><span class="line">    System.out.println(name);</span><br><span class="line">    <span class="keyword">return</span> <span class="string">&quot;page.jsp&quot;</span>;</span><br><span class="line">&#125;  </span><br></pre></td></tr></table></figure>

<ul>
<li><strong>Session数据设置（了解）</strong></li>
</ul>
<p>   名称： @SessionAttributes<br>   类型： 类注解<br>   位置：处理器类上方<br>   作用：声明放入session范围的变量名称，适用于Model类型数据传参<br>   范例：</p>
  <figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Controller</span></span><br><span class="line"><span class="meta">@SessionAttributes(names=&#123;&quot;name&quot;&#125;)</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">ServletController</span> &#123;</span><br><span class="line">    <span class="meta">@RequestMapping(&quot;/setSessionData2&quot;)</span></span><br><span class="line">    <span class="keyword">public</span> String <span class="title function_">setSessionDate2</span><span class="params">(Model model)</span> &#123;</span><br><span class="line">        model.addAttribute(<span class="string">&quot;name&quot;</span>, <span class="string">&quot;Jock2&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span> <span class="string">&quot;page.jsp&quot;</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;  </span><br></pre></td></tr></table></figure>

<ul>
<li><strong>注解式参数数据封装底层原理</strong></li>
</ul>
<p>   数据的来源不同，对应的处理策略要进行区分<br>   Head<br>   Cookie<br>   Session<br>   SpringMVC使用策略模式进行处理分发<br>   顶层接口： HandlerMethodArgumentResolver<br>   实现类： ……  </p>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="https://gitee.com/tgreen/myl-mm.git">myl</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://gitee.com/tgreen/myl-mm.git/2022/05/09/springMVCday01/">https://gitee.com/tgreen/myl-mm.git/2022/05/09/springMVCday01/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://gitee.com/tgreen/myl-mm.git" target="_blank">阿毛的博客</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/myl-mm/tags/springMVC/">springMVC</a></div><div class="post_share"><div class="social-share" data-image="https://itcast-haoke-myl.oss-cn-beijing.aliyuncs.com/images/2022/04/13/cat1.jpg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/gh/overtrue/share.js@master/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/myl-mm/2022/05/09/springMVCday02/"><img class="prev-cover" src="https://itcast-haoke-myl.oss-cn-beijing.aliyuncs.com/images/2022/04/13/cat1.jpg" onerror="onerror=null;src='/myl-mm/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">SpringMVC-day02</div></div></a></div><div class="next-post pull-right"><a href="/myl-mm/2022/05/09/spring-day04/"><img class="next-cover" src="https://itcast-haoke-myl.oss-cn-beijing.aliyuncs.com/images/2022/04/13/cat1.jpg" onerror="onerror=null;src='/myl-mm/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">Spring-day04</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/myl-mm/2022/05/09/springMVCday03/" title="SpringMVC-day03"><img class="cover" src="https://itcast-haoke-myl.oss-cn-beijing.aliyuncs.com/images/2022/04/13/cat1.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-05-09</div><div class="title">SpringMVC-day03</div></div></a></div><div><a href="/myl-mm/2022/05/09/springMVCday02/" title="SpringMVC-day02"><img class="cover" src="https://itcast-haoke-myl.oss-cn-beijing.aliyuncs.com/images/2022/04/13/cat1.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-05-09</div><div class="title">SpringMVC-day02</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="https://i.loli.net/2021/02/24/5O1day2nriDzjSu.png" onerror="this.onerror=null;this.src='/myl-mm/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">myl</div><div class="author-info__description"></div></div><div class="card-info-data site-data is-center"><a href="/myl-mm/archives/"><div class="headline">文章</div><div class="length-num">22</div></a><a href="/myl-mm/tags/"><div class="headline">标签</div><div class="length-num">11</div></a><a href="/myl-mm/categories/"><div class="headline">分类</div><div class="length-num">7</div></a></div><a id="card-info-btn" href="https://gitee.com/tgreen/"><i class="fab fa-github"></i><span>Follow Me</span></a></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">欢迎观看，又来一个！</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#1-SpringMVC-%E6%A6%82%E8%BF%B0"><span class="toc-number">1.</span> <span class="toc-text">1 SpringMVC 概述</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#2-%E5%85%A5%E9%97%A8%E6%A1%88%E4%BE%8B"><span class="toc-number">2.</span> <span class="toc-text">2 入门案例</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#2-1-%E5%85%A5%E9%97%A8%E6%A1%88%E4%BE%8B%E5%88%B6%E4%BD%9C"><span class="toc-number">2.1.</span> <span class="toc-text">2.1 入门案例制作</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-2-%E5%85%A5%E9%97%A8%E6%A1%88%E4%BE%8B%E5%B7%A5%E4%BD%9C%E6%B5%81%E7%A8%8B%E5%88%86%E6%9E%90"><span class="toc-number">2.2.</span> <span class="toc-text">2.2 入门案例工作流程分析</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-3-SpringMVC-%E6%8A%80%E6%9C%AF%E6%9E%B6%E6%9E%84%E5%9B%BE"><span class="toc-number">2.3.</span> <span class="toc-text">2.3 SpringMVC 技术架构图</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#3-%E5%9F%BA%E6%9C%AC%E9%85%8D%E7%BD%AE"><span class="toc-number">3.</span> <span class="toc-text">3 基本配置</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#3-1-%E5%B8%B8%E8%A7%84%E9%85%8D%E7%BD%AE%EF%BC%88Controller%E5%8A%A0%E8%BD%BD%E6%8E%A7%E5%88%B6%EF%BC%89"><span class="toc-number">3.1.</span> <span class="toc-text">3.1 常规配置（Controller加载控制）</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#3-1-1-%E9%9D%99%E6%80%81%E8%B5%84%E6%BA%90%E5%8A%A0%E8%BD%BD"><span class="toc-number">3.1.1.</span> <span class="toc-text">3.1.1 静态资源加载</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-1-2-%E4%B8%AD%E6%96%87%E4%B9%B1%E7%A0%81%E5%A4%84%E7%90%86"><span class="toc-number">3.1.2.</span> <span class="toc-text">3.1.2 中文乱码处理</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-2-%E6%B3%A8%E8%A7%A3%E9%A9%B1%E5%8A%A8"><span class="toc-number">3.2.</span> <span class="toc-text">3.2 注解驱动</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#4-%E8%AF%B7%E6%B1%82"><span class="toc-number">4.</span> <span class="toc-text">4 请求</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#4-1-%E6%99%AE%E9%80%9A%E7%B1%BB%E5%9E%8B%E5%8F%82%E6%95%B0%E4%BC%A0%E5%8F%82"><span class="toc-number">4.1.</span> <span class="toc-text">4.1 普通类型参数传参</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-2-POJO%E7%B1%BB%E5%9E%8B%E5%8F%82%E6%95%B0%E4%BC%A0%E5%8F%82"><span class="toc-number">4.2.</span> <span class="toc-text">4.2 POJO类型参数传参</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-3-%E6%95%B0%E7%BB%84%E4%B8%8E%E9%9B%86%E5%90%88%E7%B1%BB%E5%9E%8B%E5%8F%82%E6%95%B0%E4%BC%A0%E5%8F%82"><span class="toc-number">4.3.</span> <span class="toc-text">4.3 数组与集合类型参数传参</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-4-%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2%E5%99%A8"><span class="toc-number">4.4.</span> <span class="toc-text">4.4 类型转换器</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-5-%E6%97%A5%E6%9C%9F%E7%B1%BB%E5%9E%8B%E6%A0%BC%E5%BC%8F%E8%BD%AC%E6%8D%A2"><span class="toc-number">4.5.</span> <span class="toc-text">4.5 日期类型格式转换</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-6-%E8%87%AA%E5%AE%9A%E4%B9%89%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2%E5%99%A8"><span class="toc-number">4.6.</span> <span class="toc-text">4.6 自定义类型转换器</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-7-%E8%AF%B7%E6%B1%82%E6%98%A0%E5%B0%84-RequestMapping"><span class="toc-number">4.7.</span> <span class="toc-text">4.7 请求映射 @RequestMapping</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#4-7-1-%E6%96%B9%E6%B3%95%E6%B3%A8%E8%A7%A3"><span class="toc-number">4.7.1.</span> <span class="toc-text">4.7.1 方法注解</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-7-2-%E7%B1%BB%E6%B3%A8%E8%A7%A3"><span class="toc-number">4.7.2.</span> <span class="toc-text">4.7.2 类注解</span></a></li></ol></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#5-%E5%93%8D%E5%BA%94"><span class="toc-number">5.</span> <span class="toc-text">5 响应</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#5-1-%E9%A1%B5%E9%9D%A2%E8%B7%B3%E8%BD%AC"><span class="toc-number">5.1.</span> <span class="toc-text">5.1 页面跳转</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#5-2-%E9%A1%B5%E9%9D%A2%E8%AE%BF%E9%97%AE%E5%BF%AB%E6%8D%B7%E8%AE%BE%E5%AE%9A-InternalResourceViewResolver"><span class="toc-number">5.2.</span> <span class="toc-text">5.2 页面访问快捷设定 (InternalResourceViewResolver)</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#5-3-%E5%B8%A6%E6%95%B0%E6%8D%AE%E9%A1%B5%E9%9D%A2%E8%B7%B3%E8%BD%AC"><span class="toc-number">5.3.</span> <span class="toc-text">5.3 带数据页面跳转</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#5-4-%E8%BF%94%E5%9B%9Ejson%E6%95%B0%E6%8D%AE"><span class="toc-number">5.4.</span> <span class="toc-text">5.4 返回json数据</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#6-Servlet%E7%9B%B8%E5%85%B3%E6%8E%A5%E5%8F%A3-Servlet%E7%9B%B8%E5%85%B3%E6%8E%A5%E5%8F%A3%E6%9B%BF%E6%8D%A2%E6%96%B9%E6%A1%88"><span class="toc-number">6.</span> <span class="toc-text">6 Servlet相关接口-Servlet相关接口替换方案</span></a></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/myl-mm/2022/05/09/RabbitMQ%20Windows%20%E5%AE%89%E8%A3%85/" title="RabbitMQ Windows 安装"><img src="https://itcast-haoke-myl.oss-cn-beijing.aliyuncs.com/images/2022/04/13/cat1.jpg" onerror="this.onerror=null;this.src='/myl-mm/img/404.jpg'" alt="RabbitMQ Windows 安装"/></a><div class="content"><a class="title" href="/myl-mm/2022/05/09/RabbitMQ%20Windows%20%E5%AE%89%E8%A3%85/" title="RabbitMQ Windows 安装">RabbitMQ Windows 安装</a><time datetime="2022-05-09T09:04:30.600Z" title="发表于 2022-05-09 17:04:30">2022-05-09</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/myl-mm/2022/05/09/gateway%E5%86%85%E7%BD%AE%E8%BF%87%E6%BB%A4%E5%99%A8%E5%B7%A5%E5%8E%82/" title="Spring Cloud Gateway 内置的过滤器工厂"><img src="https://itcast-haoke-myl.oss-cn-beijing.aliyuncs.com/images/2022/04/13/cat1.jpg" onerror="this.onerror=null;this.src='/myl-mm/img/404.jpg'" alt="Spring Cloud Gateway 内置的过滤器工厂"/></a><div class="content"><a class="title" href="/myl-mm/2022/05/09/gateway%E5%86%85%E7%BD%AE%E8%BF%87%E6%BB%A4%E5%99%A8%E5%B7%A5%E5%8E%82/" title="Spring Cloud Gateway 内置的过滤器工厂">Spring Cloud Gateway 内置的过滤器工厂</a><time datetime="2022-05-09T09:04:19.065Z" title="发表于 2022-05-09 17:04:19">2022-05-09</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/myl-mm/2022/05/09/Turbine%E6%90%AD%E5%BB%BA%E6%AD%A5%E9%AA%A4/" title="Turbine"><img src="https://itcast-haoke-myl.oss-cn-beijing.aliyuncs.com/images/2022/04/13/cat1.jpg" onerror="this.onerror=null;this.src='/myl-mm/img/404.jpg'" alt="Turbine"/></a><div class="content"><a class="title" href="/myl-mm/2022/05/09/Turbine%E6%90%AD%E5%BB%BA%E6%AD%A5%E9%AA%A4/" title="Turbine">Turbine</a><time datetime="2022-05-09T09:04:13.000Z" title="发表于 2022-05-09 17:04:13">2022-05-09</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/myl-mm/2022/05/09/Eureka%20%E5%85%A8%E9%83%A8%E9%85%8D%E7%BD%AE%20/" title="Euraka配置详解"><img src="https://itcast-haoke-myl.oss-cn-beijing.aliyuncs.com/images/2022/04/13/cat1.jpg" onerror="this.onerror=null;this.src='/myl-mm/img/404.jpg'" alt="Euraka配置详解"/></a><div class="content"><a class="title" href="/myl-mm/2022/05/09/Eureka%20%E5%85%A8%E9%83%A8%E9%85%8D%E7%BD%AE%20/" title="Euraka配置详解">Euraka配置详解</a><time datetime="2022-05-09T09:03:49.699Z" title="发表于 2022-05-09 17:03:49">2022-05-09</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/myl-mm/2022/05/09/01-Springboot-day01/" title="springBootday01"><img src="https://itcast-haoke-myl.oss-cn-beijing.aliyuncs.com/images/2022/04/13/cat1.jpg" onerror="this.onerror=null;this.src='/myl-mm/img/404.jpg'" alt="springBootday01"/></a><div class="content"><a class="title" href="/myl-mm/2022/05/09/01-Springboot-day01/" title="springBootday01">springBootday01</a><time datetime="2022-05-09T09:03:15.476Z" title="发表于 2022-05-09 17:03:15">2022-05-09</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2022 By myl</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/myl-mm/js/utils.js"></script><script src="/myl-mm/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.js"></script><div class="js-pjax"></div><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>